package org.asociacion.backend.persistence.dao.impl;

import java.util.List;

import org.asociacion.backend.persistence.dao.IndexImageDao;
import org.asociacion.backend.persistence.entities.TblIndexImage;
import org.asociacion.backend.persistence.impl.GenericDaoImpl;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class IndexImageDaoImpl extends GenericDaoImpl<TblIndexImage, Integer> implements IndexImageDao {

	@Autowired
	public IndexImageDaoImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}

	@Override
	public TblIndexImage getImage(String folder, String name) {
		Criteria criteria = getCriteria().createAlias("tblIndexImageProperties", "properties")
				.add(Restrictions.eq("name", name))
				.add(Restrictions.eq("properties.folder", folder));
		return (TblIndexImage)criteria.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TblIndexImage> getListImage(String folder, boolean pattern) {
		Criteria criteria = getCriteria().createAlias("tblIndexImageProperties", "properties");
		if (pattern) {
			criteria.add(Restrictions.like("properties.folder", folder, MatchMode.START));
		} else {
			criteria.add(Restrictions.eq("properties.folder", folder));
		}
		return (List<TblIndexImage>)criteria.list();
	}

}
